﻿using System;
using System.Web.UI.WebControls;
using SiteTest.BL;
using SiteTest.Classes;
using SiteTest.Data;
using Speed;
using Speed.Data;

namespace SiteTest
{

    public partial class Default : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string where = null; // aqui ou num textbox vc pode incluir um filtro. Nao é obrigatório ter filtro
                grid.SortDirection2 = SortDirection.Ascending;
                grid.SortExpression2 = "CustomerId";
                pager.PageSize = 10;
                pager.TotalItems = (int)BL_Customers.Count(Db, where);
                pager.MoveFirst();
            }
        }

        protected void pager_PageNumberChanged(object o, SiteTest.WebControls.DataPagerEventArgs e)
        {
            string sort = grid.SortSql;
            grid.DataSource = BL_Customers.SelectPage(Db, e.From, pager.PageSize, sort);
            grid.DataBind();
        }

        protected void grid_Sorting(object sender, GridViewSortEventArgs e)
        {
            pager.MoveFirst();
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            using (var db = Sys.NewDb())
            {
                // Exemplo de uso de transaction
                db.BeginTransaction();

                int count = 10000;
                var max = BL_Customers.GetLastId(db);

                TimerCount tc = new TimerCount("Records: " + count);

                for (int i = 0; i < count; i++)
                {
                    var rec = new Customer();
                    rec.CustomerName = "Name " + (++max);
                    BL_Customers.Save2(db, rec);
                }
                // tc.Next("Other");
                // ...

                db.Commit();

                string time = tc.ToString();
                //throw new Exception(time);
                time.ToString(); // breakpoint

            }
        }

    }

}